package com.clkj.nangang.mvp.quarters;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Toast;

import com.clkj.nangang.R;
import com.clkj.nangang.app.CustomApplication;
import com.clkj.nangang.mvp.base.BaseActivity;
import com.clkj.nangang.mvp.model.XiaoquModel;
import com.clkj.nangang.util.XListView;
import com.clkj.nangang.util.schedulers.SchedulerProvider;

import java.util.ArrayList;
import java.util.List;

//选择小区或组
public class QuartersActivity extends BaseActivity implements QuartersContract.View, XListView.IXListViewListener {

    private String urbanRural;
    private XListView xlistview;

    private QuartersContract.Presenter presenter;

    //private List<XiaoquModel> xiaoquModels = new ArrayList<>();
    private List<String> xiaoquStrs = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_quarters);
        initTitle(null, null, "选择列表", true, null);
        initData();
        initView();
        assignView();

        presenter.getQuarters(urbanRural);
    }

    @Override
    public void initData() {
        urbanRural = getIntent().getStringExtra("type");

        presenter = new QuartersPresenter(this, SchedulerProvider.getInstance(), CustomApplication.getInstance().getHttpService());
    }

    @Override
    public void assignView() {

    }

    @Override
    public void initView() {

        xlistview = (XListView) findViewById(R.id.xlistview);
        xlistview.setPullLoadEnable(false);
        xlistview.setPullRefreshEnable(false);
        //xlistview.setXListViewListener(this);
    }

    @Override
    public void onNetError(String tip) {
        Toast.makeText(this, tip, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void getQuartersSuccess(List<XiaoquModel> xiaoquModels) {

        xiaoquStrs.clear();
        for (XiaoquModel xiaoquModel : xiaoquModels) {
            xiaoquStrs.add(xiaoquModel.getName());
        }

        ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, R.layout.xiaoqu_listviewitem, xiaoquStrs);

        xlistview.setAdapter(arrayAdapter);

        xlistview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                Intent it = new Intent();
                it.putExtra("xiaoqu", xiaoquStrs.get(position - 1));
                setResult(RESULT_OK, it);
                finish();

            }
        });
    }

    @Override
    public void getQuartersFail(String tip) {
        Toast.makeText(this, tip, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void setPresenter(QuartersContract.Presenter presenter) {
        this.presenter = presenter;
    }

    //刷新和加载
    private void onStopLoad() {
        xlistview.stopRefresh();
        xlistview.stopLoadMore();
    }

    @Override
    public void onRefresh() {

    }

    @Override
    public void onLoadMore() {

    }
}
